package com.fingard.dsp.bank.directbank.ccb41;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ICancelAuthorizeForRec;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.*;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.detail.RetAtsQryRecAuthCancelInfoDetail;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.RetAtsQryRecAuthInfoDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/8/6.
 */
public class CCB41AuthrizeCancelForRec extends CCB41Base implements ICancelAuthorizeForRec {
    @Override
    public RetAtsAuthCancelForRec d_AuthCancel(ReqAuthCancelForRec p_req) throws Exception {
        RetAtsAuthCancelForRec retAts = new RetAtsAuthCancelForRec(p_req.reqHeader);
        if(p_req.readRecord()){
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                CCB41Header header = new CCB41Header();
                header.tranId ="0004";
                header.tranDate = Format.Date8Format.format(new Date());
                String compId = getBankConfig().getMapValueByKey("PROTOCOL",p_req.ownAct);
                header.compId=compId;
                header.seqNo = p_req.reqSeqID;
                writeCommonHeader(xmlWriter,header);
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("Name",p_req.readedDetail.oppActName,true);//姓名
                xmlWriter.writeElementString("Idcardno",p_req.readedDetail.certNum,true);//证件号码
                xmlWriter.writeElementString("Phone",p_req.readedDetail.reqReserved4,true);//手机号
                xmlWriter.writeElementString("Account",p_req.readedDetail.oppAct,true);//账号
                String Max_amt = getBankConfig().getMapValueByKey("Max_amt",p_req.ownAct);
                xmlWriter.writeElementString("Max_amt",Max_amt,true);//单笔限额
                xmlWriter.writeElementString("Reserve1","",true);//保留1
                xmlWriter.writeElementString("Reserve2","",true);//保留2
                xmlWriter.writeElementString("Reserve3","",true);//保留3
                xmlWriter.writeElementString("Reserve4","",true);//保留4
                xmlWriter.writeElementString("Reserve5","",true);//保留5
                xmlWriter.writeEndDocument();
                String fileName = String.format("%1$s%2$s_AuthCancelInfo.txt",getBankConfig().getValueAsString("saveDir"),p_req.readedDetail.oppAct);
                //项目编号|申请流水id|name|证件号码|手机号|账号|收方账号
                StringBuilder context = new StringBuilder();
                context.append(compId).append("|").append(p_req.reqSeqID).append("|").append( p_req.readedDetail.oppActName).append("|")
                        .append(p_req.readedDetail.certNum).append( "|" ).append(p_req.readedDetail.reqReserved4).append("|").append(p_req.readedDetail.oppAct)
                        .append("|").append(p_req.ownAct);
                synchronized (CCB41AuthrizeCancelForRec.class) {
                    FileHelper.writeFile(fileName, context.toString(), getCharset());
                }
                String[] bankResult = sendToBank(xmlWriter.toXmlString());
                if(bankResult[0].length()==0){
                    StringBuilder tmpbkMsg= new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1],tmpbkMsg);
                    if(tmpbkReader!=null){
                        String retcode = tmpbkReader.getFirstText("/root/Retcode");
                        String retmsg = tmpbkReader.getFirstText("/root/Retmsg");
                        retAts.respCode = "0";
                        if("0000".equals(retcode)){
                            retAts.reqSeqID = p_req.reqSeqID;
                            retAts.retCode = "2";
                        }else{
                            retAts.retCode = "1";
                        }
                        retAts.respInfo = retmsg;
                    }else{
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpbkMsg.toString();
                    }
                }else {
                    retAts.respCode ="-1";
                    retAts.respInfo =bankResult[1];
                }
            }catch (Exception e){
                retAts.respCode ="-1";
                retAts.respInfo = e.getMessage();
                e.printStackTrace();
            }
        }else {
            retAts.respCode ="-1";
            retAts.respInfo = "detail信息为空";
        }
        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryAuthCancelForRec d_qry_AuthCancel(ReqQryAuthCancelForRec p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryRecAuthCancelInfo d_qry_AuthCancelInfo(ReqQryRecAuthCancelInfo p_req) throws Exception {
        RetAtsQryRecAuthCancelInfo retAts = new RetAtsQryRecAuthCancelInfo(p_req.reqHeader);
        retAts.ownAct = p_req.ownAct;
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            String signCancelFile = String.format("%1$s%2$s_AuthCancelInfo.txt", getBankConfig().getValueAsString("saveDir"), p_req.oppAct);
            //项目编号|申请流水id|name|证件号码|手机号|账号|收方账号
            String authInfoStr;
            synchronized (CCB41AuthrizeCancelForRec.class) {
                authInfoStr = FileHelper.readFileText(signCancelFile, getCharset());
            }
            if (StringHelper.isNullOrEmpty(authInfoStr)) {
                throw new Exception("签约会员文件不存在或者文件内容为空");
            }
            //项目编号|申请流水id|name|证件号码|手机号|账号|收方账号
            String[] authInfo = authInfoStr.split("\\|");
            String compid = getBankConfig().getMapValueByKey("PROTOCOL", p_req.ownAct);
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            CCB41Header header = new CCB41Header();
            header.tranId = "0003";
            header.tranDate = Format.Date8Format.format(new Date());
            header.compId = compid;
            header.seqNo = authInfo[1];
            writeCommonHeader(xmlTextWriter, header);
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeElementString("Signid", "", true);//签约会员号
            xmlTextWriter.writeElementString("Name", authInfo[2], true);//姓名
            xmlTextWriter.writeElementString("Idcardno", authInfo[3], true);//证件号码
            xmlTextWriter.writeElementString("Phone", authInfo[4], true);//手机号
            xmlTextWriter.writeElementString("Account", authInfo[5], true);//账号
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString());
            if (bankResult[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                XmlTextReader tmpbkReader = loadBankRespXmlReader(bankResult[1], tmpbkMsg);
                if (tmpbkReader != null) {
                    String retcode = tmpbkReader.getFirstText("/Root/Head/Retcode");
                    String retmsg = tmpbkReader.getFirstText("/Root/Head/Retmsg");
                    if ("0000".equals(retcode)) {
                        RetAtsQryRecAuthCancelInfoDetail tmpDetail = new RetAtsQryRecAuthCancelInfoDetail();
                        String Signstat = tmpbkReader.getFirstText("/Root/Body/Signstat");
                        if ("01".equals(Signstat)) {
                            retAts.respCode = "0";
                            retAts.respInfo = "解约成功";
                            tmpDetail.authDateFrom = tmpbkReader.getFirstText("/Root/Body/Signdate");
                            tmpDetail.oppAct = tmpbkReader.getFirstText("/Root/Body/Account");
                            tmpDetail.oppActName = tmpbkReader.getFirstText("/Root/Body/Name");
                            tmpDetail.queryTag = p_req.queryTag;
                            retAts.writeRetDetail(tmpDetail);
                        } else if ("07".equals(Signstat) || "00".equals(Signstat) || "08".equals(Signstat) || "04".equals(Signstat) || "05".equals(Signstat)) {
                            tmpRespCode = "0";
                            tmpRespInfo = "解约失败";
                        } else {
                            tmpRespCode = "0";
                            tmpRespInfo = "状态异常：" + Signstat;
                        }
                    } else {
                        tmpRespCode = "0";
                        tmpRespInfo = retmsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpbkMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = bankResult[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsQryRecAuthCancelInfo(p_req.reqHeader);
            retAts.respCode = tmpRespCode;
            retAts.respInfo = tmpRespInfo;
            retAts.ownAct = p_req.ownAct;
        } else {
            retAts.respCode = tmpRespCode;
            retAts.respInfo = tmpRespInfo;
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}
